// pages/changePass/changePass.js
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
import config from '../../utils/config'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        oldPass: '',
        newPass: '',
        newPassConfirm: '',
        oldErrMsg: '',
        newErrMsg: '',
        newErrMsgConfirm: '',
        flag1: true,
        flag2: true,
       userId:''
    },
    changePass () {
        if (this.data.newPassConfirm !== this.data.newPass) {
            Notify({ type: 'danger', message: '两次密码不一致！' });
            this.setData({
                newPass: '',
                newPassConfirm: ''
            })
            return;
        }
        let that=this
        wx.getStorage({
            key: 'userInfo',
            success: function(res) {     
              that.data.userId=res.data.userId;
              wx.request({
                url: `${config.baseUrl}/changePass`,
                method: 'POST',
                data: {
                    oldPass:that.data.oldPass,
                    newPass:that.data.newPassConfirm,
                    userId:that.data.userId
                },
                success:(res)=>{
                   if (res.data.code==200) {
                    Notify({ type: 'success', message:res.data.msg  ,});
                   }else{
                    Notify({ type: 'danger', message:res.data.msg  });
                    return
                   }
                   setTimeout(()=>{
                    wx.navigateTo({
                        url: '/pages/login/login',
                      })
                   },2000)
                  
                    console.log(res);
                }
            })
            },
            fail: function(res) {
              console.log('获取数据失败');
            }
          });
        
    },
    openpsone() {
        this.setData({
            flag: !this.data.flag
        })
    },
    openpsthree() {
        this.setData({
            flag2: !this.data.flag2
        })
    },
    openpstwo() {
        this.setData({
            flag1: !this.data.flag1
        })
    },
    checkPassword(pass) {
        const passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{8,}$/;
        return passwordRegex.test(pass);
    },
    getOldPass(e) {
        if (e.detail.value == '') {
            this.setData({
                oldErrMsg: '请输入原密码！'
            })
        } else if (!this.checkPassword(e.detail.value)) {
            this.setData({
                newErrMsg: '密码格式不对'
            })
        }
        else {
            this.setData({
                oldPass: e.detail.value,
                oldErrMsg: ''
            })
        }
    },
    getNewPass(e) {
        if (e.detail.value == '') {
            this.setData({
                newErrMsg: '请输入新密码！'
            })
        } else if (!this.checkPassword(e.detail.value)) {
            this.setData({
                newErrMsg: '密码由数字和字母组成，至少八位'
            })
        }
        else {
            this.setData({
                newPass: e.detail.value,
                newErrMsg: ''
            })
        }
    },
    getNewPassConfirm(e) {
        console.log(456);
        if (e.detail.value == '') {
            this.setData({
                newErrMsgConfirm: '请再次输入新密码！'
            })
        }
        else if (!this.checkPassword(e.detail.value)) {

            this.setData({
                newErrMsgConfirm: '密码由数字和字母组成，至少八位'
            })
        }
        else {
            this.setData({
                newErrMsgConfirm: '',
                newPassConfirm: e.detail.value,
            })
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})